// 赢牌动画
class winEffect extends egret.Sprite{
    public ON_CLOSE:string = "onClose";
    private modelStr:string;
    private timer;
    public constructor (model:string){
        super();
        this.modelStr = model;
        this.once(egret.Event.ADDED_TO_STAGE,this.onAdded,this);
        this.once(egret.Event.REMOVED_FROM_STAGE,this.onRemove,this);
    }
    private action:egret.MovieClip;
    private ziPic:egret.Bitmap;
    private onRemove(){       
        if (this.ziPic) {
            egret.Tween.removeTweens(this.ziPic)
        }
        if(this.timer){
            egret.clearTimeout(this.timer);
        }     
    }
    private soundtype:string;
    private onAdded(){
        // this.action = MyLibrary.createMovieClipByName("winAction",true);
        // this.addChild(this.action);
        // this.action.visible = false;
        if(this.modelStr == "bu_card"){
            this.soundtype = "buzhang";
            this.ziPic = MyLibrary.createBitmapByName("buAction_png",true);
            this.startEffect();
        }
        if(this.modelStr == "peng"){
            this.soundtype = "peng";
            this.ziPic = MyLibrary.createBitmapByName("winZi_peng_png",true);
            this.startEffect();
        }
        // if(this.modelStr == "peng"){
        //     this.soundtype = "peng";
        //     this.ziPic = MyLibrary.createBitmapByName("winZi_peng_png",true);
        //     this.startEffect();
        // }
        if(this.modelStr == "gang" || this.modelStr == "gonggang"){
            this.soundtype = "gang";
            this.ziPic = MyLibrary.createBitmapByName("winZi_gang_png",true);
            this.startEffect();
        }
        if(this.modelStr == "angang"){
            this.soundtype = "gang";
            this.ziPic = MyLibrary.createBitmapByName("winZi_gang_png",true);
            this.startEffect();
        }
        if(this.modelStr == "hu"){
            this.soundtype = "hu";
            this.ziPic = MyLibrary.createBitmapByName("winZi_hu_png",true);
            this.startEffect();
        }
        if(this.modelStr == "liuju"){
            this.ziPic = MyLibrary.createBitmapByName("winZi_liuju_png",true);
            this.startEffect();
        }
        if(this.modelStr == "chi"){
            this.soundtype = "chi";
            this.ziPic = MyLibrary.createBitmapByName("chiAction_png",true);
            this.startEffect();
        }
        if(this.modelStr == "zimo"){
            this.soundtype = "zimo";
            this.ziPic = MyLibrary.createBitmapByName("zimo-text_png",true);
            this.startEffect();
        }
        if(this.modelStr == "dianpao"){
            this.soundtype = "dianpao";
            this.ziPic = MyLibrary.createBitmapByName("dianpao-text_png",true);
            this.startEffect();
        }
        if(this.modelStr == "fangpao"){
            this.soundtype = "fangpao";
            this.ziPic = MyLibrary.createBitmapByName("fangpao-text_png",true);
            setTimeout(() => {
                this.startEffect();
            }, 2000)
        }
        const pattern = /dasixi|liuliushun|banbanhu|queyise/
        if (this.modelStr.match(pattern)) {
            this.ziPic = MyLibrary.createBitmapByName(this.modelStr + "_png",true);
            this.startEffect();
        }
        const pattern2 = /_png$/
        if (this.modelStr.match(pattern2)) {
            this.ziPic = MyLibrary.createBitmapByName(this.modelStr,true);
            this.startEffect();
        }
    }
    private startEffect(){
        // this.ziPic.scaleX = this.ziPic.scaleY = 1.5;
        const pattern2 = /_png$/
        if (this.modelStr.match(pattern2)) {
            this.ziPic.scaleX = this.ziPic.scaleY = 2;
        }
        this.ziPic.x = 0;
        this.ziPic.y = 0;
        // this.ziPic.alpha = 0;
        this.addChild(this.ziPic);

        const patternHasEffect = /hu|dianpao|fangpao|zimo/
        if (this.modelStr.match(patternHasEffect)) {
            this.ziPic.scaleX = this.ziPic.scaleY = 3;        
            egret.Tween.get(this.ziPic).to({x:0,y:0,scaleX:1,scaleY:1,alpha:1},400,egret.Ease.quartIn).call(() => {
                this.playerEffect()
                egret.Tween.removeTweens(this.ziPic)
            })
        } else {
            this.ziPic.scaleX = this.ziPic.scaleY = 1  
            this.playerEffect()         
            // egret.Tween.get(this.ziPic).call(() => {
            //     this.playerEffect()
            // })
        }
        // egret.Tween.get(this.ziPic).to({x:0,y:0,scaleX:1,scaleY:1,alpha:1},300,egret.Ease.quartIn).call(()=>{
    }

    private playerEffect() {
        const pattern2 = /_png$/
        if(this.soundtype == "peng"){
            StageData.soundObj.soundSinglePlay("win_peng");
            StageData.soundObj.soundSinglePlay("outCard");
        }
        if(this.soundtype == "chi"){
            StageData.soundObj.soundSinglePlay("chi");
            StageData.soundObj.soundSinglePlay("outCard");
        }
        if(this.soundtype == "gang"){
            StageData.soundObj.soundSinglePlay("win_gang");
            StageData.soundObj.soundSinglePlay("outCard");
        }
        if(this.soundtype == "hu"){
            StageData.soundObj.soundSinglePlay("win_hu");
            StageData.soundObj.soundSinglePlay("outCard");
        }
        if(this.soundtype == "liuju"){
            StageData.soundObj.soundSinglePlay("win_liuju");
            StageData.soundObj.soundSinglePlay("outCard");
        }
        if(this.soundtype == "buzhang"){
            StageData.soundObj.soundSinglePlay("buzhang");
            StageData.soundObj.soundSinglePlay("outCard");
        }
        // if(this.soundtype == "dianpao"){
        //     StageData.soundObj.soundSinglePlay("win_dianpao");
            StageData.soundObj.soundSinglePlay("outCard");
        // }
        if(this.soundtype == "zimo"){
            StageData.soundObj.soundSinglePlay("win_zimo");
            StageData.soundObj.soundSinglePlay("outCard");
        }
        if(this.soundtype == "fangpao"){
            // StageData.soundObj.soundSinglePlay("win_fangpao");
            // StageData.soundObj.soundSinglePlay("outCard");
        }
        if (this.modelStr.match(pattern2)) {
            StageData.soundObj.soundSinglePlay("win_hu");
            StageData.soundObj.soundSinglePlay("outCard");
        }
        // this.action.play();
        // this.action.visible = true;
        this.timer = egret.setTimeout(()=>{
            egret.clearTimeout(this.timer);
            this.dispatchEventWith(this.ON_CLOSE);
        },this,500);   
        egret.setTimeout(()=>{
            const pattern = /dasixi|liuliushun|banbanhu|queyise|liuju/
            console.log('this.modelStr', this.modelStr);
            if (this.modelStr.match(pattern) && this.ziPic.parent) {
                this.removeChild(this.ziPic)
            }
        },this,2000);  
        egret.setTimeout(()=>{
            const pattern = /hu|dianpao|fangpao|zimo/
            if (this.modelStr.match(pattern) && this.ziPic.parent) {
                this.removeChild(this.ziPic)
            }
        }, this, 5000)
    }
}